Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: "",
    canIUse_edit: true,
    company_id: 0,    //绑定得企业ID
    status: -1,      //用户状态
    qrcode: '',      //揽件二维码 
    statusArr: ["待审核", "在职", "离职", "驳回"],
    company_type: 0,   //企业类型；0：物流、1：快递
    scene: '',
    company_name: '',
    company_id: '',
    company_type: -1,
    companyList: []
  },

  /**
   * 跳转个人中心
   */
  toPerson: function () {
    wx.navigateTo({
      url: '/pages/person/person',
    })
  },

  /**
   * 跳转揽件数据统计
   * @param {*} options 
   */
  toStatistics: function () {
    wx.navigateTo({
      url: '/pages/statistics/statistics',
    })
  },

  toCustomGoodInfo() {
    let companyInfo = wx.getStorageSync('companyInfo'),company_id,company_type;
    if(companyInfo){
      company_id = companyInfo.company_id;
      company_type = companyInfo.company_type;
    }
    wx.navigateTo({
      url: '/pages/customGoodInfo/customGoodInfo?company_id=' + company_id + '&company_type=' + company_type,
    })
  },

  /**
  * 跳转揽件数据统计
  * @param {*} options 
  */
  toWaybill: function () {
    wx.navigateTo({
      url: '/pages/waybill/waybill',
    })
  },

  scope_userInfo() {
    let that = this;
    wx.getSetting({
      success: res => {
        console.log(res)
        that.setData({
          canIUse_edit: res.authSetting['scope.userInfo'] || false
        })
      }
    })
  },

  saveImage(e) {
    let url = e.currentTarget.dataset.url;
    wx.saveImage(url);
  },

  changeCompany(e) {
    let {
      company_name,
      company_id,
      company_type,
      qrcode
    } = this.data.companyList[e.detail.value] || {}

    let data = {
      company_name: company_name,
      company_id: company_id,
      company_type: company_type,
      qrcode: qrcode
    }
    this.setData(data)
    wx.setStorageSync('companyInfo', data)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.scope_userInfo();
    this.setData({
      scene: options.scene,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    this.initIndex();
  },

  async getOpen(){
    await getApp().login();
  },

  async initIndex(){
    if(!wx.getStorageSync('login')){
      await this.getOpen();
      console.log('company_id', wx.getStorageSync('company_id'),'status', wx.getStorageSync('status'));
    }
    await this.initCheck();
    console.log('come index');
    await this.getBindCompanyList();
    console.log('come index22222');

  },

  initCheck(){
    let that = this;
    return new Promise((resolve, reject ) => {
      that.setData({
        company_id: wx.getStorageSync('company_id'),
        status: wx.getStorageSync('status'),
        // qrcode: wx.getStorageSync('qrcode'),
        // company_type: wx.getStorageSync('company_type')   
      });
      console.log('init_check','company_id', that.data.company_id, 'status', that.data.status);
      if (!that.data.company_id || that.data.status == 2) {      //如果不存在企业ID，或者处于离职状态得话，则跳转到新增用户那里去，理论上是弹出一个禁止界面的，因为不能通过揽件员自己新增，而要企业邀请员工
        let go_url = '/pages/addPerson/addPerson';
        if (that.data.scene != undefined) {
          go_url = go_url + '?scene=' + that.data.scene;
          wx.reLaunch({
            url: go_url,
          })
        } else {
          wx.navigateTo({
            url: '/pages/verify/verify?content=' + '请通过扫描企业邀请二维码进入...' + '&title=请扫码',
          })
        }
      }
      if (that.data.company_id && that.data.status == 0) {      //如果没有揽件二维码，说明还在审核中，则不能继续提交审核
        wx.navigateTo({
          url: '/pages/verify/verify?content=' + '正在审核中...' + '&title=正在审核中',
        })
      }
      resolve();
    });
    
  },

  /**
   * 获取绑定的企业列表
   */
  getBindCompanyList(){
    let that = this;
    wx.$api.getBindCompany().then(res => {
      if (res.data.code == 200) {
        let companyInfo = {}
        let companyList =res.data.data || [];
        if (wx.getStorageSync('companyInfo')) {
          that.changeCompanyInfo(wx.getStorageSync('companyInfo'), companyList);
        } else {
          companyInfo = companyList[0]
          wx.setStorageSync('companyInfo', companyList[0])
          that.setData({
            companyList: companyList,
            company_name: companyInfo.company_name,
            company_id: companyInfo.company_id,
            company_type: companyInfo.company_type,
            qrcode:companyInfo.qrcode
          })
        }
      }else{
        wx.$Toast(res.data.msg)
      }
    })
  },

  /**
   * 改变公司状态
   * @param {*} companyInfo 
   */
  changeCompanyInfo(companyInfo, companyList){
    wx.$api.checkCompany({
      company_id: companyInfo.company_id
    }).then(res => {
      if (res.data.code == 200) {
        let info = res.data.data || {};
        companyInfo.company_type = info.type;
        let index = 0;
        companyList = companyList.map((item,i) => {
          if (item.company_id == info.company_id) {
            item.company_type = info.type
          }
          if(item.company_id == companyInfo.company_id){
            index=i;
          }
          return item
        })
        this.changeCompany({
          detail:{
            value:index
          }
        })
        this.setData({
          companyList: companyList,
          company_name: companyInfo.company_name,
          company_id: companyInfo.company_id,
          company_type: companyInfo.company_type,
          qrcode:companyInfo.qrcode
        })
        wx.setStorageSync('companyInfo', companyInfo)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})
